#include "test_uart.h"
#include "cm_iomux.h"
#include "string.h"


///测试添加
static char uart_event_param[20] = {0};
static char uart_event_cb_param[50] = {0};
static unsigned int uart_event = 0;
#define UART_EVENT(event) #event
void cm_test_uart_event_entry(void *param,unsigned int evt)
{
    strcpy(uart_event_cb_param,param);
    uart_event = evt;
    // cm_demo_printf("param:%s, event:%d\n",param,evt);
    // if(evt & CM_UART_EVENT_TYPE_RX_ARRIVED)
    // {
    //     cm_demo_printf("%s\n",UART_EVENT(CM_UART_EVENT_TYPE_RX_ARRIVED));
    // }
    // if(evt & CM_UART_EVENT_TYPE_RX_OVERFLOW)
    // {
    //     cm_demo_printf("%s\n",UART_EVENT(CM_UART_EVENT_TYPE_RX_OVERFLOW));
    // }
    // if(evt & CM_UART_EVENT_TYPE_TX_COMPLETE)
    // {
    //     cm_demo_printf("%s\n",UART_EVENT(CM_UART_EVENT_TYPE_TX_COMPLETE));
    // }
}

void cm_test_uart_cfg(unsigned char *cmd[],int len)
{
    cm_uart_byte_size_e byte_size = atoi(cmd[2]); //8,7,6,5
    cm_uart_parity_e parity = atoi(cmd[3]);       //0,1,2,3,4      /*!< 校验位，枚举*/
    cm_uart_stop_bit_e stop_bit = atoi(cmd[4]);   //0,1,2     /*!< 停止位，枚举*/
    cm_uart_flow_ctrl_e flow_ctrl = atoi(cmd[5]); //0,1   /*!< 流控制，枚举*/
    int baudrate = atoi(cmd[6]);                  //9600,19200,38400,57600,115200    /*!< 波特率，枚举*/
    int is_lpuart = atoi(cmd[10]);
    cm_uart_cfg_t uart_cfg = {
    byte_size,     /*!< 数据位，枚举*/
    parity,           /*!< 校验位，枚举*/
    stop_bit,       /*!< 停止位，枚举*/
    flow_ctrl,      /*!< 流控制，枚举*/
    baudrate,                       /*!< 波特率，枚举*/
    is_lpuart,          /*!< 1:低功耗串口(115200以内的标准波特率)；0:普通串口*/
    };
    cm_uart_dev_e dev = atoi(cmd[7]);//1,2,3   (ML302三路串口UART0\1\2 使用dev值0\1\2；ML305三路串口UART 1\2\3 使用dev值1\2\3;ML307A两路串口UART0\1 使用dev值0\1)
    strcpy(uart_event_param,cmd[8]);
    void *event_param = uart_event_param;
    cm_uart_event_type_e event_type = atoi(cmd[9]);//1,2,4;1|2|4=7
    cm_uart_event_t event = {
    event_type, /*!< 事件类型，数据可读/写完成/错误等*/
    event_param,             /*!< 事件参数*/
    cm_test_uart_event_entry,               /*!< 事件执行入口*/
    };
#if CM_OPENCPU_MODEL_ML307A
    if(CM_UART_DEV_1 == dev)
    {
        cm_iomux_set_pin_func(28, CM_IOMUX_FUNC_FUNCTION1);
        cm_iomux_set_pin_func(29, CM_IOMUX_FUNC_FUNCTION1);
    }
    if(CM_UART_DEV_0 == dev)
    {
        cm_iomux_set_pin_func(17, CM_IOMUX_FUNC_FUNCTION1);
        cm_iomux_set_pin_func(18, CM_IOMUX_FUNC_FUNCTION1);
    }
    if(CM_UART_DEV_2 == dev)
    {
        cm_iomux_set_pin_func(22, CM_IOMUX_FUNC_FUNCTION3);
        cm_iomux_set_pin_func(23, CM_IOMUX_FUNC_FUNCTION3);
    }
#endif
#if CM_OPENCPU_MODEL_ML305A
    if(CM_UART_DEV_1 == dev)
    {
        cm_iomux_set_pin_func(2, CM_IOMUX_FUNC_FUNCTION2);
        cm_iomux_set_pin_func(3, CM_IOMUX_FUNC_FUNCTION2);
    }
    if(CM_UART_DEV_0 == dev)
    {
        cm_iomux_set_pin_func(31, CM_IOMUX_FUNC_FUNCTION1);
        cm_iomux_set_pin_func(32, CM_IOMUX_FUNC_FUNCTION1);
    }
    if(CM_UART_DEV_2 == dev)
    {
        cm_iomux_set_pin_func(49, CM_IOMUX_FUNC_FUNCTION3);
        cm_iomux_set_pin_func(54, CM_IOMUX_FUNC_FUNCTION3);
    }
#endif
    cm_demo_printf("uart cm_uart_register_event ret:%d\n", cm_uart_register_event(dev, &event));
    cm_demo_printf("uart cm_uart_open ret:%d\n", cm_uart_open(dev, &uart_cfg));
}

void cm_test_uart_operation(unsigned char *cmd[],int len)
{
    if (0 == strcmp("WRITE",cmd[2]))
    {
        cm_uart_dev_e dev = atoi(cmd[3]);
        const void *data = "test uart write";
        int32_t len = 15;
        // const char data[5] = {0x55,0x55,0x55,0x00,0x99};
        // int32_t len = 5;
        int32_t timeout = 20;
        cm_demo_printf("uart cm_uart_write ret:%d\n", cm_uart_write(dev, data, len, timeout));
    }else if (0 == strcmp("READ",cmd[2]))
    {
        cm_uart_dev_e dev = atoi(cmd[3]);
        char data[101] = {0};
        int32_t len = 100;
        int32_t timeout = 20;
        cm_demo_printf("uart cm_uart_read ret:%d\n",  cm_uart_read( dev,  data,  len,  timeout));
        cm_uart_write(dev, data, len, timeout);
        cm_demo_printf("read data:%s\n",data);
    }else if (0 == strcmp("CLOSE",cmd[2]))
    {
        cm_uart_dev_e dev = atoi(cmd[3]);
        cm_demo_printf("uart cm_uart_close ret:%d\n",  cm_uart_close( dev));
    }else if(0 == strcmp("EVT_PRINT",cmd[2]))
    {
        cm_demo_printf("param:%s, event:%d\n",uart_event_cb_param,uart_event);
    }
    else{
        cm_demo_printf("param error!\n");
    }
}

